clear
clear matrix
set more off
cd "~add working directory here"

use poq_covidretro_replication, replace

* Clean variables

for any 1 2: replace year_est_X = . if pctile_year_X>.95 | pctile_year_X<.05
for any 1 2: replace month_est_X = . if pctile_month_X>.95 | pctile_month_X<.05
for any 2: replace day_est_X = . if pctile_day_X>0.95 | pctile_day_X<.05


for any 1 2: recode crisis_est_X 1=1 0/.5=0, g(pctcrisisworse_X)
for any 1 2: recode trump_handle_X 4/5=1 1/3=0, g(trump_handle_well_X)


g crisis_worse_nov = crisis_est_2 - crisis_est_1
recode crisis_worse_nov -.5=-1 .5=1

foreach x in 1 2 {
	
	recode candidate_approve_`x' -6/-1=-1 1/6=1, g(net_approve_`x')
	
}

foreach x in 1 2 {

replace candidate_approve_`x' = (candidate_approve_`x'/6)

}

foreach x in 1 2 {

replace trump_handle_`x' = ((trump_handle_`x' - 3)/2)
replace votetrump_`x' = (votetrump_`x')

}


foreach x in 1 2 {

replace candidate_approve_`x' = 1 if candidate_approve_`x'>0 & candidate_approve_`x'!=.
replace candidate_approve_`x' = 0 if candidate_approve_`x'<0 & candidate_approve_`x'!=.

}


foreach x in 1 2 {

replace trump_handle_`x' = 1 if trump_handle_`x' >0 & trump_handle_`x'!=.
replace trump_handle_`x' = 0 if trump_handle_`x' <0 & trump_handle_`x'!=.

}



****************
*** FIGURE 1 ***
****************

table votetrump_1, stat(mean year_est_1 month_est_1 pctcrisisworse_1)
table votetrump_2, stat(mean year_est_2 month_est_2 day_est_2 pctcrisisworse_2)

****************
*** FIGURE 2 ***
****************

* Worse
sum votetrump_1 votetrump_2 trump_handle_1 trump_handle_2 candidate_approve_1 candidate_approve_2 if crisis_est_1<1 & crisis_worse_nov==1

* Same
sum votetrump_1 votetrump_2 trump_handle_1 trump_handle_2 candidate_approve_1 candidate_approve_2 if crisis_est_1<1 & crisis_worse_nov==0

***************
*** TABLE 1 ***
***************

*Flip crisis_est; high value = crisis better

for any 1 2: replace crisis_est_X = 1 - crisis_est_X

* Controlling Demographics *
foreach x in candidate_approve_ votetrump_ trump_handle_ {
 
	
	reg `x'2  `x'1 crisis_est_1, beta
	eststo bi1_`x'
	
	reg crisis_est_2 crisis_est_1 `x'1 , beta
	eststo bi2_`x'
	
	
	reg `x'2  `x'1 crisis_est_1 white black latino ba polknow_avg age, beta
	eststo multi1_`x'
	
	reg crisis_est_2 crisis_est_1 `x'1  white black latino ba polknow_avg age, beta
	eststo multi2_`x'
	
	reg `x'2  `x'1 crisis_est_1 white black latino ba polknow_avg age party7, beta
	eststo multip1_`x'
	
	reg crisis_est_2 crisis_est_1 `x'1  white black latino ba polknow_avg age party7, beta
	eststo multip2_`x'

}


esttab bi2_votetrump_ multi2_votetrump_ multip2_votetrump_ bi1_votetrump_ multi1_votetrump_ multip1_votetrump_, replace beta p(2) b(2) nomtitle nostar varlabels(crisis_est_1 "W1 Crisis Est." votetrump_1 "W1 Vote Trump" white "White" black "Black" latino "Latinx" ba "BA+" polknow_avg "Pol. Knwldg" age "Age" party7 "Party ID") compress


***************
*** TABLE 2 ***
***************

* Categories hand-coded. See SM8 for coding details and dataset variable "attribution_open"
